package com.itlong.cloud.property.service.impl;


import com.itlong.cloud.POJO.DTO.property.PropertyGroupGetPageDTO;
import com.itlong.cloud.POJO.PO.PropertyGroupPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyGroupDao;
import com.itlong.cloud.property.service.IPropertyGroupService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      部门业务接口实现类
 * </desc>
 *
 * @createDate 2017/10/17
 */
@Service
public class PropertyGroupServiceImpl implements IPropertyGroupService {

    @Autowired
    IPropertyGroupDao iPropertyGroupDao;
    /**
     * <desc>
     *      保存部门
     * </desc>
     * @param propertyGroupPO   待添加的物业部门对象
     * @return  添加数量
     * @author Biao.R
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer save(PropertyGroupPO propertyGroupPO) throws Exception {
        Integer status = propertyGroupPO.getStatus();
        propertyGroupPO.setGroupId(LogicIdUtil.bussinessId());
        propertyGroupPO.setCreateTime(new Date());
        propertyGroupPO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyGroupPO.setStatus(status == null ? DataStatusEnum.NORMAL.getType() : status);
        String maxNum = iPropertyGroupDao.getMaxNum(propertyGroupPO.getProjectId());
        //前置补零
        propertyGroupPO.setGroupNum(String.format("%03d", Integer.valueOf(maxNum) + 1));
        int result = iPropertyGroupDao.save(SqlUtil.durableData(propertyGroupPO, PlatformConstants.TABLE_SAVE));
        return result;
    }

    /**
     * <desc>
     *      更新部门
     * </desc>
     * @param   propertyGroupPO   待更新的物业部门对象
     * @return  更新数量
     * @author Biao.R
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(PropertyGroupPO propertyGroupPO) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("groupName",propertyGroupPO.getGroupName());
        params.put("projectId",propertyGroupPO.getProjectId());
        params.put("groupIdNot",propertyGroupPO.getGroupId());
        params.put("statusNot",'0');
        List<PropertyGroupPO> groupList = iPropertyGroupDao.getByParams(params);
        if(groupList !=null && !groupList.isEmpty()){
            return -1;
        }

        propertyGroupPO.setUpdateTime(new Date());
        propertyGroupPO.setProjectId(propertyGroupPO.getProjectId());
        return iPropertyGroupDao.update(SqlUtil.durableData(propertyGroupPO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     * @param   pageDTO  部门分页查询对象
     * @return  查询到的部门集合
     * @author Biao.R
     * @createDate 2017/10/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyGroupPO> getPage(PropertyGroupGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<PropertyGroupPO> page=new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("projectId",pageDTO.getProjectId());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iPropertyGroupDao.getPage(params), iPropertyGroupDao.getPageCount(params));
        if (page.getRows() != null && !page.getRows().isEmpty()) {
            page.getRows().get(0).setAllUserCount(iPropertyGroupDao.getAllGroupUserCount(params));
        }
        return page;
    }

    /**
     * <desc>
     *      根据id查询部门
     * </desc>
     *
     * @param   propertyGroupPO 部门实体
     * @return  查询到的部门
     * @author Biao.R
     * @createDate 2017/09/27
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyGroupPO getOne(PropertyGroupPO propertyGroupPO) {
        return iPropertyGroupDao.getOne(propertyGroupPO);
    }

    /**
     * <desc>
     *      将人员分配至部门
     * </desc>
     *
     * @param groupId 部门id
     * @param allotUsers 待分配的用户id 多个，号分隔
     * @param userIds 当前列表所有用户id 多个，号分隔
     * @return  添加个数
     * @author Biao.R
     * @createDate 2017/10/10
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveGroupUsers(String groupId, String allotUsers, String userIds) {
        //先将用户的角色删除
        if (StringUtils.isNotEmpty(userIds)) {
            iPropertyGroupDao.deleteGroupUsers(StringHandlerUtil.splitString(userIds), groupId);
        }
        if(StringUtils.isNotEmpty(allotUsers)){
        //为用户分配角色
        Map<String,Object> params=new HashMap<>();
        params.put("allotUsers",StringHandlerUtil.splitString(allotUsers));
        params.put("updateTime",new Date());
        params.put("groupId",groupId);
        return iPropertyGroupDao.saveGroupUsers(params);
        }
        return 1;
    }

    /**
     * <desc>
     *      根据字段查询部门
     * <desc/>
     * @param   params  查询参数
     * @return List<PropertyGroupPO>    返回对象集合
     * @author Biao.R
     * @createDate 2017/10/18
     */
    @Override
    public List<PropertyGroupPO> getByFeilds(Map<String, Object> params) {
        return iPropertyGroupDao.getByParams(params);
    }
}
